package com.ffapp.rainmonitorcustom.login;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;


import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisActivity;
import com.ffapp.rainmonitorcustom.bean.LoginServiceListData;
import com.ffapp.rainmonitorcustom.util.MyUtils;

import butterknife.BindView;
import butterknife.OnClick;
import my.CheckUtils;


public class SeverSettingActivity extends BasisActivity {

    @BindView(R.id.edit_ip)
    EditText edit_ip;
//    @BindView(R.id.edit_port)
//    EditText edit_port;


    @Override
    public void initViews() {
        setDefautTrans(false);
        super.initViews();
        setContentView(R.layout.login_service_setting_activity);
        setTitleBg(R.drawable.base_transparent);
//        setTitleLeftButton(null);

        edit_ip.setHint("请输入服务器地址(包含端口号)");
        edit_ip.setText("http://192.168.25.48:1080/");
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        LoginServiceListData loginServiceListData = MyUtils.getSerciceData();
        if(loginServiceListData != null){
            edit_ip.setText(loginServiceListData.getIp());
        }

    }


    @OnClick(R.id.btn_ok)
    void checkAndPost() {
        String ip = edit_ip.getText().toString().trim();
//        String port = edit_port.getText().toString().trim();

        if (CheckUtils.isStrEmpty(ip)) {
            showToast("请输入IP");
            return;
        }

//        if (CheckUtils.isStrEmpty(port)) {
//            showToast("请输入端口号");
//            return;
//        }

        if(!ip.startsWith("http")){
            ip = "http://"+ip;
        }
        if(!ip.endsWith("/")){
            ip = ip +"/";
        }


        LoginServiceListData loginServiceListData = new LoginServiceListData();
        loginServiceListData.setIp(ip);
//        loginServiceListData.setPort(port);
        loginServiceListData.setName("默认");
        loginServiceListData.save();
        MyUtils.putSerciceData(loginServiceListData);
        setResult(Activity.RESULT_OK);
        finish();
    }

}
